import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import ViteVue from '@vitejs/plugin-vue'
import VitePages from 'vite-plugin-pages'
import ViteMarkdown from 'vite-plugin-md'
import ViteComponents from 'unplugin-vue-components/vite'
import markdownItAnchor from 'markdown-it-anchor'
import markdownItPrism from 'markdown-it-prism'
import Unocss from 'unocss/vite'
import { presetUno, presetAttributify, presetIcons } from 'unocss'
import fs from 'fs-extra'
import { resolve } from 'path'
import matter from 'gray-matter'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    ViteVue({ include: [/\.vue$/, /\.md$/] }),
    ViteMarkdown({
      wrapperClasses: ['write', 'markdown-body'],
      markdownItSetup(md) {
        // add anchor links to your H[x] tags
        md.use(markdownItAnchor)
        // add code syntax highlighting with Prism
        md.use(markdownItPrism, {
          highlightInlineCode: true
        })
      }
    }),
    VitePages({
      extensions: ['vue', 'md'],
      pagesDir: 'docs',
      extendRoute(route) {
        const path = resolve(__dirname, route.component.slice(1))
        const md = fs.readFileSync(path, 'utf-8')
        const { data } = matter(md)
        route.meta = Object.assign(route.meta || {}, { frontmatter: data })
        return route
      }
    }),
    ViteComponents({
      include: [/\.vue$/, /\.md$/]
    }),
    Unocss({
      presets: [presetUno(), presetAttributify(), presetIcons()]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})
